package org.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.example.common.JwtHelper;
import org.example.common.Result;
import org.example.common.ResultCodeEnum;
import org.example.pojo.Admin;
import org.example.pojo.Query;
import org.example.service.AdminService;
import org.example.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author 13326
* @description 针对表【hards_admin】的数据库操作Service实现
* @createDate 2024-12-28 16:30:43
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
    private final AdminMapper adminMapper;

    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(Admin admin){
        //创建一个查询对象，查询admin类里面username=admin.username的数据
    LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(Admin::getUsername,admin.getUsername());
    Admin admin1=adminMapper.selectOne(wrapper);
//        System.out.println(admin1.getUsername());
        if(admin1 == null){
            System.out.println("用户不存在");
            return Result.build("用户不存在", ResultCodeEnum.USERNAME_ERROR);
        }
        if(StringUtils.isEmpty(admin.getPassword())||!admin1.getPassword().equals(admin.getPassword())){
            System.out.println("密码错误");
      return Result.build("密码错误",ResultCodeEnum.PASSWORD_ERROR);
        }
//        if(StringUtils.isEmpty(admin.getRole())||!admin.getRole().equals(admin1.getRole())){
//            System.out.println("该用户不存在");
//            return Result.build("该用户不存在",ResultCodeEnum.NOROLE);
//        }
        System.out.println("登录成功");
        Map<String, Object> map=new HashMap<>();
        String token=jwtHelper.createToken(Long.valueOf(admin1.getId()));
        map.put("userinfo",admin1);
        map.put("token",token);

        return Result.ok(map);
}
@Override
    public Result register(Admin admin){
        LambdaQueryWrapper<Admin> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getUsername,admin.getUsername());
        if(adminMapper.selectCount(lambdaQueryWrapper)>0){
            return Result.build(505,ResultCodeEnum.USERNAME_USED);

        }
        adminMapper.insert(admin);
        return Result.ok("注册成功");

}
@Override
    public Result getlist(Query query){
        LambdaQueryWrapper<Admin> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getUsername,query.getKeyword());
    IPage<Admin> page=new Page<>(query.getPageNum(),query.getPageSize());
    adminMapper.selectPageMap(page,query);
    HashMap<String,Object> hashMap=new HashMap<>();
    hashMap.put("total",page.getTotal());
    hashMap.put("rows",page.getRecords());
    hashMap.put("pages",page.getPages());
    hashMap.put("size",page.getSize());
    return Result.ok(hashMap);


}
@Override
    public Result add(Admin admin){
        int rows=adminMapper.insert(admin);
        return Result.ok("成功");
}
@Override
    public Result updateadmin(Admin admin){
        int rows=adminMapper.updateById(admin);
        return Result.ok("修改成功");
}
@Override
    public Result deleteadmin(Integer id){
        adminMapper.deleteById(id);
        return Result.ok("删除成功");
}
}




